package com.zzptc.zzsgz.bdsjws.activities;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zzptc.zzsgz.bdsjws.R;
import com.zzptc.zzsgz.bdsjws.activities.base.BaseActivity;
import com.zzptc.zzsgz.bdsjws.com.GuideAdapter;
import com.zzptc.zzsgz.bdsjws.utils.MyUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 引导页面
 */
public class GuideActivity extends BaseActivity {
    List<View> lists = new ArrayList<View>();
    @BindView(R.id.vp_guide)
    ViewPager vpGuide;
    @BindView(R.id.linlay)
    LinearLayout linlay1;
   @BindView(R.id.tv_skip)
   TextView tv_Skip;

    //下面是要用ViewPage+PageAdapter 完成页面滑动的效果
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public int getContentXML() {
        return R.layout.activity_guide;
    }

    /**
     * 组织数据源
     */
    @Override
    public void initData() {
        //画三个圆圈
        View point1 = null;
        for (int i = 0; i < 3; i++) {
            point1 = new View(this);
            point1.setBackgroundResource(R.drawable.point_select);
            point1.setEnabled(false);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
            params.leftMargin = 20;
            linlay1.addView(point1, params);//加入布局中

        }
        linlay1.getChildAt(0).setEnabled(true);//选择第一个点 并设置它的Enabled属性为true

        //三张布局放入集合中
        View view1 = View.inflate(this, R.layout.activity_guide_one, null);
        View view2 = View.inflate(this, R.layout.activity_guide_two, null);
        View view3 = View.inflate(this, R.layout.activity_guide_three, null);


        lists.add(view1);
        lists.add(view2);
        lists.add(view3);
        GuideAdapter g = new GuideAdapter(lists);
        vpGuide.setAdapter(g);

        //按钮在view3
        Button btn_welcome = (Button) view3.findViewById(R.id.btn_welcome);
        btn_welcome.setOnClickListener(new View.OnClickListener() {
            //进入手机卫士
            @Override
            public void onClick(View view) {
                //设置共享首选项
                SharedPreferences s = MyUtil.getSharedPre();
                SharedPreferences.Editor edits = s.edit();//获取编辑器
                edits.putBoolean("wel", true);//一旦设为true  下次进入就不会到引导页面
                edits.commit();
                //跳转
                startActivity(new Intent(GuideActivity.this, WelcomeActivity.class));
                finish();
            }
        });
//监听：点击跳过文本框时，要跳到第三个滑动页面上去
        tv_Skip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vpGuide.setCurrentItem(lists.size()-1);
            }
        });
//滑动页面的监听
        vpGuide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //控制圆圈
                for (int i = 0; i < lists.size(); i++) {
                    if (i == position) {
                        linlay1.getChildAt(i).setEnabled(true);
                    } else {
                        linlay1.getChildAt(i).setEnabled(false);
                    }
                }
//控制“跳过”文本框    在第三个滑动页面中让文本框隐藏
                switch (position){
                    case 0:
                        tv_Skip.setVisibility(View.VISIBLE);
                        break;
                    case 1:
                        tv_Skip.setVisibility(View.VISIBLE);
                        break;
                    case 2:
                        tv_Skip.setVisibility(View.INVISIBLE);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public void initView() {

    }
}
